(ns clj-8583.core
  (:gen-class)
  (:require [clojure.java.io :as io :only resource])
  (:use [clojure.pprint]
        [clj-8583.server]
        [clj-8583.binary]
        [clj-8583.format]
        [clj-8583.format-iso8583]
        [clj-8583.crypt]
        [clj-8583.decompose]
        [clj-8583.business]
        [clj-8583.response]
        ))


(def config (clojure.edn/read-string (slurp "resources/config.edn")))

(defn char-array-to-int
  [buf]
  (Integer/parseInt (bytes-to-str buf)))

;(str/replace str #"\[|\]|\n" "")

(defn read-int
  [reader]
  (let [buflen (byte-array 4)]
    (.read reader buflen 0 4)
    (prn (. java.time.LocalTime now))
    ;(prn "----------------------")
    ;(prn (bytes-to-str buflen))
    ;(prn "----------------------")
    (char-array-to-int buflen)))

(def field-defs field-definitions)

; (defn response-message-process
;   [field-maps]
;   field-maps)
(def mac-k1 (atom (slurp "resources/keys.edn")))
; (def mac-k1 (decrypt-mac-key mac-key main-key))

(defn response-handler
  [buf writer]
  (let [field-maps ((parser field-defs) buf)
        ;sec-code (if (contains? #{"0800" "0810"} (:message-type field-maps)) (:message-cecurity-code field-maps) mac-key)
        ;mac-k (decrypt-mac-key sec-code main-key)]
        mac-k (if (contains? #{"0800" "0810"} (:message-type field-maps)) (decrypt-mac-key (:message-cecurity-code field-maps) main-key) @mac-k1)]
    (def ^:dynamic f-maps field-maps)
    (def f-maps (assoc f-maps :response-code "00"))
    (def f-maps (assoc f-maps :message-type (response-message-type (:message-type field-maps))))
    (when-not (= mac-k @mac-k1)
      (do
        (spit "resources/keys.edn" mac-k)
        (reset! mac-k1 mac-k)))
    (prn f-maps)
    (prn "before when-not message-type")
    (when-not (= (:message-type f-maps) "0810")
      (def f-maps (business-process f-maps)))
    ;(def f-maps field-maps)
    ; (if (mac-validate field-maps field-definitions message-definitions mac-k)
    ;   (def f-maps (assoc f-maps :response-code "00"))
    ;   (def f-maps (assoc f-maps :response-code "11")))
    (prn f-maps)
    (prn "response-hander 1" (. java.time.LocalTime now))
    (let [mess-bytes (message-bytes f-maps field-definitions message-definitions mac-k)]
      (prn "response-hander 2" (. java.time.LocalTime now))
      (.write writer mess-bytes)
      (.flush writer))))

(defn handler [reader writer]
  (loop []
    (prn "-==========================")
    (prn "started....... " (. java.time.LocalTime now))
    (let [buflen (read-int reader)
          buf (byte-array buflen)]
      (prn buflen)
      (prn "read ended ......." (. java.time.LocalTime now))
      (if (> buflen 0)
        (do
          (.read reader buf 0 buflen)
          (prn (bytes-to-hex buf))
          (response-handler (byte-array buf) writer)
          (prn "handler ended ........" (. java.time.LocalTime now)))
        (do
          (.write writer (.getBytes "0000"))
          (.flush writer))))
    (prn (. java.time.LocalTime now))
    ;(Thread/sleep 10)
    (recur)))


(def server
  (tcp-server
    :port    (:serv-port config)
    :handler (wrap-streams handler)))



(defn -main
  "I don't do a whole lot."
  [& args]
  ;[x]
  ;(println x "Hello, World!"))
  ;(prn "Hello" ", World!")
  (start server)
  (prn "The server is running? " (running? server)))
